#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@File    :   contraband.py    
@Contact :   fengfeng.qiu@amh-group.com

@Modify Time      @Author    
------------      -------    
2022/2/10 17:44   qiufengfeng      
'''
from locust import HttpUser,task,between
import json
import pandas as pd
import re
def processo_data(item):
    result = re.findall('[S|D]\：[^SD]*', item['calltext'])
    return list(result)
def load_test_data():
    file_path = r'./data/20220421.csv'
    df = pd.read_csv(file_path,sep="")
    df['dialgo_list'] = df.apply(processo_data,axis=1)
    return df['dialgo_list'].tolist()


dialog_texts = load_test_data()
print(len(dialog_texts))
index = 0

demo_str =  [
      "D:喂。",
      "S:嗯。",
      "D:从那个邢台沙河拉这个煤矸石到这个临清嘞。",
      "S:框装来排队的啊。",
      "D:那不好装车。"
    ]


class HelloWorldUser(HttpUser):
    wait_time = between(0.5, 1)
    host = "http://qa-beetle.amh-group.com"
    api_url = "/beetle/contraband/contraband_identify"

    # host = "https://qa-predict-phantom.amh-group.com"
    # api_url = "/contraband/v1/models/contraband:predict"
    @task
    def hello_word(self):
        global index

        text_dialog = dialog_texts.pop()
        data = {"dialogue":[text_dialog]}

        header = {"Content-Type": "application/json"}
        import json
        req = self.client.post(self.api_url,data=json.dumps(data),headers=header)
        #req = self.client.post("/phantom/tf-predict", data=json.dumps(data), headers=header)
        if req.json()['orochi_errorMsg'] == "":
            pass

        else:
            print("fails")
            print(text_dialog)
            print(req.json())
            #self.error_file_writer.write(json.dumps(text_dialog)+"\n")

    def on_start(self):
        self.error_file_writer = open('error.txt',mode='w',encoding='utf-8')

    def on_stop(self):
        self.error_file_writer.close()



